import * as types from '../mutation-types'
import userService from 'service/userService.js'
import appointService from 'service/appointService.js'
import loginService from 'service/loginService.js'
import configService from 'service/configService.js'
import {
    getHiddenText
} from '@/libs/filter.js'
const state = {
    loginUserInfo: {},
    callUserInfo: {},
    callUserRelationTheStudentInfo: {},
    appointList: [],
    roleList: []
}

const getters = {
    loginUserInfo: () => state.loginUserInfo,
    callUserInfo: () => state.callUserInfo,
    appointList: () => state.appointList,
    callUserRelationTheStudentInfo: () => state.callUserRelationTheStudentInfo,
    roleList: () => state.roleList

}
const actions = {
    //登陆人信息
    async getLoginUserInfo({ commit, dispatch }) {
        // try {
        //     let loginUserInfo = {
        //         'userId': '423', //登陆人ID
        //         "curHandlerGroupId": "23", // 操作人所属组ID
        //         "curHandlerGroupName": "客服组", // 操作人所属组名字
        //         "curHandlerName": "王伟", // 操作人（登陆人）名字
        //         "curHandlerNo": "3658" // 操作人工号
        //     }
        //     commit(types.LOGIN_USER_INFO, loginUserInfo)
        // } catch (err) {
        //     console.log(err)
        // }
        // let loginUserInfo = state.loginUserInfo
        // commit(types.LOGIN_USER_INFO, loginUserInfo)

        let jsondata = await userService.getLoginUserInfo({})
        dispatch('setLoginUserInfo', {
            'userId': jsondata.principal.username, //登陆人ID
            "curHandlerGroupId": jsondata.principal.organCode, // 操作人所属组ID
            "curHandlerGroupName": jsondata.principal.organName, // 操作人所属组名字
            "curHandlerName": jsondata.principal.nickName, // 操作人（登陆人）名字
            "curHandlerNo": jsondata.principal.username, // 操作人工号
            "curHandlerId": jsondata.principal.username
        })
    },
    setLoginUserInfo({ commit }, loginUserInfo) {
        commit(types.LOGIN_USER_INFO, loginUserInfo)
    },
    //设置来电人 相关的学生信息
    setCallUserInfoRelationTheStudent({ commit }, callUserRelationTheStudentInfo) {

        commit(types.CALL_USER_RELATION_STUDENT_INFO, callUserRelationTheStudentInfo)
    },
    //设置来电人信息 telNo callId IVR路径
    async setCallUserInfo({ commit }, callUserInfo) {
        //外呼号码 dnis
        if (callUserInfo.callType == 'Outbound') {
            callUserInfo.telNo = callUserInfo.dnis && callUserInfo.dnis != 'null' ? getHiddenText(callUserInfo.dnis) : ''
            callUserInfo.mobileNo = callUserInfo.dnis && callUserInfo.dnis != 'null' ? callUserInfo.dnis : ''
        } else {
            callUserInfo.telNo = callUserInfo.ani && callUserInfo.ani != 'null' ? getHiddenText(callUserInfo.ani) : ''
            callUserInfo.mobileNo = callUserInfo.ani && callUserInfo.ani != 'null' ? callUserInfo.ani : ''
        }
        callUserInfo.callID = callUserInfo.connId ? callUserInfo.connId : ''
        callUserInfo.IVR_path = callUserInfo.v_dtmf ? callUserInfo.v_dtmf : ''

        //号码归属地
        try {
            let jsondata = await configService.getLocationOfPhone({
                'mobileNumber': callUserInfo.mobileNo
            })
            if (jsondata.mobileArea) {
                callUserInfo.mobileArea = jsondata.mobileArea
            }

        } catch (err) {
            console.log('获取号码归属地')
        }



        // {'telNo': '13378789393',   'callID': '123456789'}

        commit(types.CALL_USER_INFO, callUserInfo)
    },

    //登陆人的 预约回拨列表
    async getAppointList({ commit }, data) {
        let jsondata = await appointService.getAppointList({
            'userId': data.userId
        })
        if (jsondata.state) {
            let appointList = jsondata.data
            commit(types.APPOINT_LIST, appointList)
        } else {
            let appointList = []
            commit(types.APPOINT_LIST, appointList)
        }
    },
    //登陆人 角色
    async getRoleList({ commit }, data) {
        let jsondata = await loginService.stateHome(data)

        let roleList = jsondata.roleCodeList
        commit(types.USER_ROLE_LIST, roleList)
    }

}
const mutations = {
    [types.LOGIN_USER_INFO](state, loginUserInfo) {
        state.loginUserInfo = loginUserInfo
    },
    [types.CALL_USER_INFO](state, callUserInfo) {
        //state.callUserInfo = callUserInfo
        localStorage.callUserInfo = JSON.stringify(callUserInfo)
        state.callUserInfo = localStorage.callUserInfo ? JSON.parse(localStorage.callUserInfo) : callUserInfo

    },
    getCalluserInfo(state) {
        state.callUserInfo = localStorage.callUserInfo ? JSON.parse(localStorage.callUserInfo) : {}
        console.log(state.callUserInfo)
    },
    clearCallUserInfo(state) {
        localStorage.removeItem('callUserInfo')
        state.callUserInfo = {}
    },
    [types.CALL_USER_RELATION_STUDENT_INFO](state, callUserRelationTheStudentInfo) {
        state.callUserRelationTheStudentInfo = callUserRelationTheStudentInfo
    },
    [types.APPOINT_LIST](state, appointList) {
        state.appointList = appointList
    },
    [types.USER_ROLE_LIST](state, roleList) {
        state.roleList = roleList
    }

}
export default {
    state,
    getters,
    actions,
    mutations
}